package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.service.FlowerCartService;
import com.woniu.util.R;
import com.woniu.vo.CartVoCZT;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("flowerCart")
@Api(value = "购物车接口", tags = "购物车功能")
public class FlowerCartController {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private FlowerCartService fcs;

    @ApiOperation("根据用户id查购物车列表")
    @GetMapping("selectAllCartByAccountId")
    public R selectAllCartByAccountId(int accountId, int pageNum, int pageSize) {
        Object temp = redisTemplate.opsForValue().get("cartSelectAll:" + accountId + pageNum + pageSize);
        if (temp != null) {
            return R.build(200, "从redis中查询成功",  temp);
        } else {
            PageInfo<CartVoCZT> pageInfo = fcs.selectAllCartByAccountId(accountId, pageNum, pageSize);
            redisTemplate.opsForValue().set("cartSelectAll:" + accountId + pageNum + pageSize, pageInfo);
            redisTemplate.expire("cartSelectAll:" + accountId + pageNum + pageSize, 30, TimeUnit.MINUTES);
            return R.build(200, "查询成功", pageInfo);
        }
    }

    @ApiOperation("根据购物车id改变购买数量")
    @PostMapping("updateBuyCountByCartId")
    public R updateBuyCountByCartId(int buyCount, int cartId) {
        int i = fcs.updateBuyCountByCartId(buyCount, cartId);
        if (i > 0) {
            Set<String> set = redisTemplate.keys("cartSelect*");
            if (set == null || set.size() == 0) {
                return R.success("修改成功");
            }
            redisTemplate.delete(set);
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }

    @ApiOperation("根据购物车id删除")
    @PostMapping("deleteCartByCartId")
    public R deleteCartByCartId(int cartId) {
        int i = fcs.deleteCartByCartId(cartId);
        if (i > 0) {
            Set<String> set = redisTemplate.keys("cartSelect*");
            if (set == null || set.size() == 0) {
                return R.success("删除成功");
            }
            redisTemplate.delete(set);
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @ApiOperation("多删除")
    @PostMapping("deleteMoreCart")
    public R deleteMoreCart(int[] cartIds) {
        int i = fcs.deleteMoreCart(cartIds);
        if (i > 0) {
            Set<String> set = redisTemplate.keys("cartSelect*");
            if (set == null || set.size() == 0) {
                return R.success("删除成功");
            }
            redisTemplate.delete(set);
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @ApiOperation("添加商品至用户的购物车")
    @PostMapping("insertCartByAccountId")
    public R insertCartByAccountId(int accountId,int flowerId,int buyCount) {
        int i = fcs.insertCartByAccountId(accountId,flowerId,buyCount);
        if (i > 0) {
            Set<String> set = redisTemplate.keys("cartSelect*");
            if (set == null || set.size() == 0) {
                return R.success("已添加至购物车");
            }
            redisTemplate.delete(set);
            return R.success("已添加至购物车");
        } else {
            return R.error("添加失败");
        }
    }
}
